package controller;

import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-01-19
 * Time: 下午 23:30
 */
@WebServlet("/blogDelete")
public class BlogDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String blogID = req.getParameter("blogID");

        HttpSession session = req.getSession(false);
        if(session == null) {
            resp.sendRedirect("blog_list.html");
            return;
        }

        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.sendRedirect("blog_list.html");
            return;
        }

        if(blogID == null || blogID.equals("")) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("你要删除的文章不存在!");
            return;
        }

        //删除数据库中的数据
        BlogDao blogDao = new BlogDao();
        blogDao.delete(Integer.parseInt(blogID));

        //删除完成以后返回博客列表页
        resp.sendRedirect("blog_list.html");
    }
}
